﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CalculatorFlexibe.Interfaces;

namespace CalculatorFlexibe.Implementations
{
    class SubOperation : IOperation
    {
        public string operation { get; set; }
        public int priority { get; set; }
        public IGetOperands getter { get; set; }
        public IChanger changer { get; set; }

        public SubOperation(IGetOperands getter, IChanger changer)
        {
            this.getter = getter;
            this.changer = changer;
            operation = "-";
            priority = 3;
        }

        public int FindOperationPosition(string expr)
        {
            return expr.LastIndexOf(operation);
        }
        public string Execute(string expr)
        {
            while (expr.LastIndexOf(operation) > 0)
            {
                string leftOperand = getter.GetLeftOperand(expr.LastIndexOf(operation), operation, expr);
                string rightOperand = getter.GetRightOperand(expr.LastIndexOf(operation), operation, expr);
                double result = Convert.ToDouble(leftOperand) - Convert.ToDouble(rightOperand);
                expr = changer.ChangeOnResult(expr.LastIndexOf(operation) - leftOperand.Length,
                                       expr.LastIndexOf(operation) + operation.Length + rightOperand.Length - 1,
                                       expr,
                                       result);
            }
            return expr;
        }
    }
}
